<template>
  <div class="top-menu">
    <el-menu :default-active="activeIndex"
             mode="horizontal"
             class="top_el_menu"
             text-color="#333"
             active-text-color="#409EFF">
      <el-menu-item index="0" @click.native="openHome(itemHome)" key="0">
        <template slot="title">
          <i :class="itemHome.source"></i>
          <span>{{generateTitle(itemHome)}}</span>
        </template>
      </el-menu-item>
      <template v-for="(item,index) in items">
        <el-menu-item :index="item.id+''" @click.native="openMenu(item)" :key="index">
          <template slot="title">
            <i :class="item.source" style="padding-right: 5px;"></i>
            <span>{{generateTitle(item)}}</span>
          </template>
        </el-menu-item>
      </template>
    </el-menu>
    <el-button-group class="top-menu-button">
      <el-button size="medium" type="text" icon="el-icon-caret-left" @click="moveLeft()"></el-button>
      <el-button size="medium" type="text" icon="el-icon-caret-right" @click="moveRight()"></el-button>
    </el-button-group>
  </div>
</template>

<script>
  import {mapGetters} from "vuex";

  export default {
    name: "top-menu",
    data() {
      return {
        itemHome: {
          name: '首页',
          source: 'el-icon-menu',
        },
        activeIndex: "0",
        items: [],
      };
    },
    inject: ["index"],
    created() {
      this.getMenu();
    },
    computed: {
      ...mapGetters(["tagCurrent", "menu"])
    },
    methods: {
      openHome(itemHome) {
        this.index.openMenu(itemHome);
        this.$router.push({
          path: this.$router.$avueRouter.getPath({name: itemHome.name, src: ''}, {})
        });
      },
      openMenu(item) {
        this.index.openMenu(item)
      },
      getMenu() {
        this.$store.dispatch("GetTopMenu").then(res => {
          this.items = res;
        });
      },
      generateTitle(item) {
        return this.$router.$avueRouter.generateTitle(
          item.name,
          (item.meta || {}).i18n
        );
      },
      moveLeft() {
        var topMenu = document.getElementsByClassName("top_el_menu")[0];
        topMenu.scrollLeft = topMenu.scrollLeft - 80;
      },
      moveRight() {
        var topMenu = document.getElementsByClassName("top_el_menu")[0];
        topMenu.scrollLeft = topMenu.scrollLeft + 80;
      }
    }
  };
</script>

<style lang="scss" scoped>
.top-menu {
  position: absolute;
  display: flex;
  flex-grow: 1;
  left: 60px;
  right: 380px;
  height: 52px;
  border: 1px solid #409eff;
  margin-top: 8px;
  border-radius: 25px;
}

.top_el_menu {
  position: absolute;
  left: 15px;
  right: 50px;
  overflow-y: hidden;
  overflow-x: hidden;
  display: flex;
  flex-grow: 1;
}

.top-menu-button {
  position: absolute;
  right: 10px;
  height: 50px;
  display: flex;
  justify-content: center;
  align-items: center;
}

.avue-top .el-menu--horizontal > .el-menu-item {
  height: 50px;
  line-height: 46px;
}
</style>